package model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import modelDeLivreur.Direction;
import modelDeLivreur.DirectionsManager;

public class TravelPath {
	
	/*
	 * The TravelPath class acts as a data transfer object (J2EE design pattern).
	 * It favors composition by aggregating all necessary components related to a specific 
	 * trajectory.
	 */
	public TravelPath(){
		directions = DirectionsManager.getInstance();
		polyLines = new ArrayList<String>();
	}
	
	//Accessor and mutator methods
	
	public String getStartLocation() {
		return startLocation;
	}
	public void setStartLocation(String startLocation) {
		this.startLocation = startLocation;
	}
	public String getEndLocation() {
		return endLocation;
	}
	public void setEndLocation(String endLocation) {
		this.endLocation = endLocation;
	}
	
	public void setWayPoint(String wPoint){
		wayPoints = wPoint;
	}
	
	public String getWayPoint() {
		return wayPoints;
	}
	
	public boolean addPolyLine(String pLine){
		return polyLines.add(pLine);
	}
	
	public List<String> getPolyLines() {
		return polyLines;
	}
	
	public String getPathStatus() {
		return pathStatus;
	}

	public void setPathStatus(String pathStatus) {
		this.pathStatus = pathStatus;
	}
	
	public boolean addDirection(String htmlDirection, String duration, String distance){
		return directions.addDirection(htmlDirection, duration, distance);
	}
	
	public double getTotalDistance(){
		return directions.getTotalDistance();
	}
	
	public double getTotalDuration(){
		return directions.getTotalDuration();
	}
	
	public Iterator<Direction> iterator(){
		return directions.iterator();
	}
	
	/**
	 * Method that returns TravelPath information as a string with minimal 
	 * html tags.
	 * 
	 * @return String
	 */
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("Directions: <br/>");
		sb.append("===========<br/>");
		sb.append("<b>Start (S):</b> "+getStartLocation()+"<br/>");
		sb.append("<b>End (E):</b> "+ getEndLocation()+"<br/>");
		sb.append("<b>WayPoint (W):</b> "+ getWayPoint()+"<br/></br>");
		sb.append("<b>Total duration:</b> " + getTotalDuration() + " min &nbsp;&nbsp;&nbsp;<b>Total distance:</b> "+getTotalDistance()+" mi<br/>");
		sb.append("<br/>");
		
		Iterator<Direction> it = iterator();
		for(;it.hasNext();){
			sb.append("<br>--------------------------------------------------------<br/>");
			
			Direction d = it.next();
			sb.append(d.getHtmlDirection()+"<br/>");
			sb.append(d.getDistance());
			sb.append("       ");
			sb.append(d.getDuration());
		}
		
		return sb.toString();
	}
	
	/**
	 * Method that returns TravelPath information as a html page.
	 * 
	 * @return String
	 */
	public String toHtmlString(){
		StringBuilder sb = new StringBuilder();
		
		sb.append("<html>");
		sb.append(CSS);
		sb.append("<body>");
		sb.append("<H1>Directions:</H1><br/>");
		sb.append("<H2>");
		sb.append("<b>"+getStartLocation()+"</b>");
		sb.append(" a ");
		sb.append("<b>"+getEndLocation()+"</b>");
		sb.append("<br/>");
		sb.append("Total time: ");
		sb.append(getTotalDuration());
		sb.append("       ");
		sb.append("Total distance: ");
		sb.append(getTotalDistance());
		sb.append("<br/><br/>");
		
		Iterator<Direction> it = iterator();
		for(;it.hasNext();){
			sb.append("<hr>");
			
			Direction d = it.next();
			sb.append("<b>");
			sb.append(d.getHtmlDirection());
			sb.append("</b>");
			sb.append("<br/>");
			sb.append(d.getDistance());
			sb.append(d.getDuration());
		}
		
		sb.append("</body>");
		sb.append("</html>");
		
		return sb.toString();
	}

	private static final String CSS = "<head><style type=\"text/css\">"+
	"body {margin: 0px 0px 0px 0px;padding: 0px 0px 0px 0px;font-weight: bold;font-size: 1.0em;color: #052850;text-align: left;font-style:normal;}"
	+
	"b {margin: 0px 0px 0px 0px;padding: 0px 0px 0px 0px;font-weight: bold;font-size: 1.1em;color: #052850;text-align: left;font-style:normal;}"
	+
	"div {margin: 0px 0px 0px 0px;padding: 0px 0px 0px 0px;font-weight: bold;font-size: 1.0em;color: #052850;text-align: left;font-style:normal;}"
+
	"</style></head>";
	
	private String pathStatus = null;
	private String startLocation = null;
	private String endLocation = null;
	private String wayPoints = null;
	private List<String> polyLines = null;
	private DirectionsManager directions = null;

}
